package news.web;


import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import news.model.CommonResult;
import news.model.News;
import news.model.NewsType;
import news.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Api(tags = "新闻管理")
@RestController
@RequestMapping("/admin/news")
public class NewsController {

    @Autowired
    private NewsService newsService;


    @PostMapping("/newss")
    @ApiOperation(value = "查询新闻信息")
    public CommonResult newss(@RequestBody @ApiParam(value = "新闻信息",required = true) News news){
        PageInfo<News> newsPageInfo = newsService.roleList(news);
        CommonResult commonResult = new CommonResult(200, newsPageInfo, "新闻列表查询成功");
        return commonResult;
    }

    @ApiOperation(value = "查询新闻所属类型")
    @GetMapping("/newsType")
    public CommonResult newsType(){
        List<NewsType> newsTypes = newsService.newsType();
        CommonResult commonResult = new CommonResult(200, newsTypes, "查询newsType成功");
        return commonResult;
    }

    //改变状态
    @ApiOperation(value = "改变新闻状态")
    @GetMapping("/changeStatus")
    public CommonResult changeStatus(Integer status,Integer id){
        newsService.changeStatus(status,id);
        CommonResult commonResult = new CommonResult(200, "改变状态成功");
        return commonResult;
    }

    //上传图片
    @ApiOperation(value = "新闻上传图片")
    @PostMapping("/uploadFile")
    public CommonResult uploadFile(@RequestParam("file") CommonsMultipartFile commonsMultipartFile) throws IOException {
        String newFileName = UUID.randomUUID() +commonsMultipartFile.getOriginalFilename();
        String path = "D:\\Program Files\\apache-tomcat-9.0.43\\webapps\\news\\"+newFileName;
        File file = new File(path);
        //存储
        commonsMultipartFile.transferTo(file);
        return new CommonResult(200,"http://127.0.0.1:8099/news/"+newFileName,"文件上传成功");
    }

    //保存新增得新闻
    @ApiOperation(value = "保存修改的新闻信息")
    @GetMapping("/saveNews")
    public CommonResult saveNews(@RequestBody News news,@RequestHeader String adminToken){
        if(news.getId() == null){
            newsService.saveNews(news,adminToken);
        }else{
            newsService.update(news,adminToken);
        }
        CommonResult commonResult = new CommonResult(200,"保存新闻成功");
        return commonResult;
    }

    //删除新闻
    @ApiOperation(value = "删除新闻")
    @GetMapping("/delete")
    public CommonResult delete(Integer id){
        newsService.delete(id);
        return new CommonResult(200,"删除新闻成功");
    }

    //在更新之前先把信息查询出来
    @ApiOperation(value = "通过ID查询新闻")
    @GetMapping("/findNewsById")
    public CommonResult findNewsById(int id){
        News news = newsService.findNewsById(id);
        return new CommonResult(200,news,"查询news");
    }
}

